package com.jiejie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiejie.commons.ServerResponse;
import com.jiejie.entity.Order;
import com.jiejie.entity.Seat;
import com.jiejie.mapper.OrderMapper;
import com.jiejie.mapper.SeatMapper;
import com.jiejie.service.ISeatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 辰广
 * @since 2022-10-05
 */
@Service
public class SeatServiceImpl  extends ServiceImpl<SeatMapper, Seat>  implements ISeatService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private RedisTemplate redisTemplate;



    @Override
    public ServerResponse getSeatByOrder(Integer scheduleId) {
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_status",1);
        queryWrapper.eq("schedule_id",scheduleId);
        queryWrapper.select("order_seat_num");
        List<Order> orderList=orderMapper.selectList(queryWrapper);
        StringBuilder seatstr= new StringBuilder();
        for (Order order : orderList) {
            seatstr.append(order.getOrderSeatNum()).append(",");
        }
        Set<String> keys = redisTemplate.keys("seat_"+scheduleId+"_"+"*");//
        if (keys != null) {
            List<String> listKeys = new ArrayList<>(keys);
            for (String s:listKeys) {
                seatstr.append(redisTemplate.opsForValue().get(s)).append(",");
            }
        }
        System.out.println("seatNum:"+seatstr);
        int [][]seatArr=new int[10][10];
        for (int j=0;j<seatstr.length();j+=5){
            int row= seatstr.charAt(j)-'0'-1;
            int column= seatstr.charAt(j + 2)-'0'-1;
            System.out.println(row+"==="+column);
            seatArr[row][column]=1;
        }
        return ServerResponse.success("座位数组:",seatArr);
    }
    @Override
    public boolean isSeatByOrder(Integer scheduleId,String seat) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status", 1);
        queryWrapper.eq("schedule_id", scheduleId);
        queryWrapper.like("order_seat_num",seat);
        List<Order> list= orderMapper.selectList(queryWrapper);
        System.out.println("list:"+list.size());
        return list.size()!=0;
    }

}
